import cTitle from 'components/title';
import diyBackGround from "../../../components/diyBackGround.vue";
import { debounce } from "lodash";
export default {
  data() {
    return {
      buyTimer:'',
      uploadUrl: "upload.uploadPic", //上传图片
      theme_color:'',
      username: '',
      bannerInfo:{},
      stopButton:false,
      query:{
        orders: JSON.stringify([]),
        member_coupon_ids:[],
        address:'',
        store_profit_price:'',
        store_profit_uid:'',
        store_profit_image:''
      }
    };
  },
  watch: {
  },
  methods: {
    getMembername() {//搜索会员
      let json = {
        store_profit_uid: this.query.store_profit_uid
      };
      $http.post("plugin.store-profit.frontend.index.getMemberNickname", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.username = res.username;
          } else {
            // this.$dialog.alert({ message: response.msg });
            this.$toast(response.msg);
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    bannerTo() {
      if(this.bannerInfo.web_jump) {
        window.location.href = this.bannerInfo.web_jump;
      }
    },
    profitsIndex() {
      $http.post("plugin.store-profit.frontend.index.index", {}, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.bannerInfo = res;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    onRead(e, flag) {//上传图片
      this.$toast.loading({
        message: "上传中",
        forbidClick: true
      });
      this.stopButton = true;
      let fd = new FormData();

      if (flag === true) {
        fd.append("file", e); // 第一个参数字符串可以填任意命名，第二个根据对象属性来找到file
      } else {
        fd.append("file", e.file); // 第一个参数字符串可以填任意命名，第二个根据对象属性来找到file
      }
      axios
        .post(this.uploadUrl, fd, {
          headers: { "Content-Type": "multipart/form-data" }
        })
        .then(response => {
          let responseData = response.data;
          if (responseData.result === 1) {
            this.query.store_profit_image = responseData.data.img_url;
            this.$toast.clear();
          } else {
            this.$toast(responseData.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    removeImg_1() {
      this.query.store_profit_image = '';
    },
    storeChange:debounce(function(event)  {
      if(this.query.store_profit_uid == '') return false;
      console.log(event);
      this.getMembername();
    },500),
    priceCange(event) {
      this.query.store_profit_price = this.query.store_profit_price.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3");//限制输入多个小数点和小数
    },
    initData() {
      this.username = '';
      this.query ={
        orders: JSON.stringify([]),
        member_coupon_ids:[],
        address:'',
        store_profit_price:'',
        store_profit_uid:'',
        store_profit_image:''
      }
    },
    profitsConfirm() {//提交订单
      
      if(this.fun.isTextEmpty(this.username)) {
        this.$toast('会员不存在');
        return false;
      }
      if(this.fun.isTextEmpty(this.query.store_profit_uid)) {
        this.$toast('让利会员ID错误');
        return false;
      }
      if(this.fun.isTextEmpty(this.query.store_profit_price)) {
        this.$toast('请输入让利价格');
        return false;
      }
      this.query.store_profit_price = parseFloat(this.query.store_profit_price);
      if (this.buyTimer) clearTimeout(this.buyTimer);
      this.buyTimer = setTimeout(() => {
        let profits = {uid:this.query.store_profit_uid,price:this.query.store_profit_price,image:this.query.store_profit_image};
        localStorage.setItem('profits', JSON.stringify(profits));
        this.$router.push(this.fun.getUrl("profitsPay"));
      }, 300);
    }
  },
  activated() {
    this.uploadUrl = this.fun.getRealUrl("upload.uploadPic", {});
    let basic_info = JSON.parse(localStorage.getItem("yz_basic_info"));
    this.theme_color = basic_info.theme_color;
    this.profitsIndex();
    this.initData(); // 初始化数据
  },
  components: { cTitle,diyBackGround }
};
